package com.changgou.util;

import com.changgou.file.FastDFSFile;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;


// 封装FastDFS的api工具类

/* 功能描述: TODO
 * @param: 
 * @return: 
 * @auther: YPK
 * @date: 2019/11/11 19:49
 */
public class FastDFSClient {


/* 功能描述: TODO 加载FastDFS的配置文件
 * @auther: YPK
 * @date: 2019/11/11 17:18
 */
    static {
        String path = "fdfs_client.conf";
        String config_name = new ClassPathResource(path).getPath();
        try {
            ClientGlobal.init(config_name);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    public static String[] uploadFile(FastDFSFile file) {
                /* 功能描述: TODO  附件上传
                 * @param: [file]
                 * @return: java.lang.String[]
                 * @auther: YPK
                 * @date: 2019/11/11 19:27
                 */

        try {
            // 获取附件信息
            byte[] bytes = file.getContent();
            String extname = file.getExt();
            NameValuePair [] nameValuePair = new NameValuePair[1];
            nameValuePair[0] = new NameValuePair(file.getMd5());

            // 1、创建跟踪服务器的客户端
            TrackerClient trackerClient = new TrackerClient();
            // 2、通过跟踪服务器的客户端获取服务器端
            TrackerServer trackerServer = trackerClient.getConnection();
            // 3、创建存储服务器的客户端
            StorageClient storageClient = new StorageClient(trackerServer, null);

            // 4、将附件进行上传 arg0：附件内容 arg1：附件扩展名称  arg2：附件的备注（null）
            String[] upload_file = storageClient.upload_file(bytes, extname, nameValuePair);
            return upload_file;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /* 功能描述: TODO 附件下载
     * @param: 
     * @return: 
     * @auther: YPK
     * @date: 2019/11/11 19:24
     */

    public static byte[] downloadFile(String group_name, String remote_filename) {


        try {
            TrackerClient trackerClient = new TrackerClient();
            TrackerServer trackerServer = trackerClient.getConnection();
            StorageClient storageClient = new StorageClient(trackerServer, null);
            byte[] bytes = storageClient.download_file(group_name, remote_filename);
            return  bytes;


        } catch (Exception e) {
            e.printStackTrace();
        }

        return  null;
    }


    /* 功能描述: TODO   删除附件
     * @param: [group_name, remote_filename]
     * @return: void
     * @auther: YPK
     * @date: 2019/11/11 20:19
     * @Version: 1.0
     */
    public static void deleteFile(String group_name, String remote_filename){

        try {
            // 1、创建跟踪服务器的客户端
            TrackerClient trackerClient = new TrackerClient();
            // 2、通过跟踪服务器的客户端获取服务器端
            TrackerServer trackerServer = trackerClient.getConnection();
            // 3、创建存储服务器的客户端
            StorageClient storageClient = new StorageClient(trackerServer, null);
            // 4、附件删除
            storageClient.delete_file(group_name, remote_filename);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }




    /* 功能描述: TODO 获取附件信息
     * @param: [group_name, remote_filename]
     * @return: org.csource.fastdfs.FileInfo
     * @auther: YPK
     * @date: 2019/11/11 19:30
     */

    public static FileInfo getFileInfo(String group_name, String remote_filename){

        try {
            // 1、创建跟踪服务器的客户端
            TrackerClient trackerClient = new TrackerClient();
            // 2、通过跟踪服务器的客户端获取服务器端
            TrackerServer trackerServer = trackerClient.getConnection();
            // 3、创建存储服务器的客户端
            StorageClient storageClient = new StorageClient(trackerServer, null);
            // 4、附件删除

            FileInfo file_info = storageClient.get_file_info(group_name, remote_filename);
            return  file_info;
        } catch (Exception e) {
            e.printStackTrace();
        }
            return null;
    }


    /**
     * @author 栗子
     * @Description 获取存储服务器信息
     * @Date 12:21 2019/11/11
     * @param groupName
     * @return org.csource.fastdfs.StorageServer
     **/



    /* 功能描述: TODO 获取存储服务器信息
     * @param: [groupName]
     * @return: org.csource.fastdfs.StorageServer
     * @auther: YPK
     * @date: 2019/11/11 19:34
     */
    public static StorageServer getStorageServer(String groupName){
     
        try {
            // 1、创建跟踪服务器的客户端
            TrackerClient trackerClient = new TrackerClient();
            // 2、通过跟踪服务器的客户端获取服务器端
            TrackerServer trackerServer = trackerClient.getConnection();

            // 获取存储服务器
            StorageServer storeStorage = trackerClient.getStoreStorage(trackerServer, groupName);
            return storeStorage;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }






    /* 功能描述: TODO 获取跟踪服务器地址
     * @param: []
     * @return: java.lang.String
     * @auther: YPK
     * @date: 2019/11/11 20:21
     * @Version: 1.0
     */
    public static String getUrl(){

        try {

            // 1、创建跟踪服务器的客户端
            TrackerClient trackerClient = new TrackerClient();
            // 2、通过跟踪服务器的客户端获取服务器端
            TrackerServer trackerServer = trackerClient.getConnection();
            // 3、获取端口
            int port = ClientGlobal.getG_tracker_http_port();
            // 4、获取ip地址
            String ip = trackerServer.getInetSocketAddress().getAddress().getHostAddress();

            String url = "http://" + ip + ":" + port;
            return url;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }



}
